<?php

namespace app\test\controller;



use app\test\model\Mes;

use App\User;
use think\Controller;
use think\facade\Log;
use think\facade\View;
use think\Request;

class Mess extends Controller
{
    //跳转增加页面
    public function add()
    {
        return View::fetch('add');
    }
    //增加图片
    public function createAdd(Request $request)
    {
        $file=$request->file('file');
        $info=$file->move('./uploads');
        $img='/uploads/'.$info->getSaveName();
        return json($img);
    }
    //增加功能
    public function create(Request $request)
    {
        $arr['username']=$request->post('username');
        $arr['salary']=$request->post('salary');
        $arr['company']=$request->post('company');
        $arr['professional']=$request->post('professional');
        //后台验证
        $validate = new \app\index\validate\Mes;
        $result = $validate->check($arr);
        if(!$result){
            return json(['code'=>400,'msg'=>$validate->getError()]) ;
        }
        $imgs=$request->post('img');
        $image = \think\Image::open('.'.$imgs);
// 给原图左上角添加水印并保存water_image.png
        $image->text('上海全栈',getcwd().'/simfang.ttf',40,'#ff0000')->save('./uploads/'.time().'text_image.png');
        $arr['img']='/uploads/'.time().'text_image.png';
        $user=new Mes();
        $res=$user->table('mess')->insert($arr);
        if ($res){
            Log::write('添加成功');
            return json(['code'=>200,'msg'=>'添加成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'添加失败','data'=>'']);
        }
    }
    //展示功能
    public function show(Request $request)
    {
        $keyword=$request->get('keyword');
        if ($keyword){
            $user=new Mes();
            $data=$user->table('mess')->where('username','like',"%$keyword%")->select();
            return View::fetch('show',['data'=>$data]);
        }else{
            $user=new Mes();
            $data=$user->table('mess')->order('salary','desc')->select();
            return View::fetch('show',['data'=>$data]);
       }

    }
    //软删除
    public function del(Request $request)
    {
        $id=$request->get('id');
       $user=new Mes();
       $res=Mes::destroy($id);
       if ($res){
           Log::write('删除成功');
           return json(['code'=>200,'msg'=>'删除成功','data'=>$res]);
       }else{
           return json(['code'=>400,'msg'=>'删除失败','data'=>'']);
       }
    }
}
